import { isFunction } from "lodash";
import { reactive, toRaw, type Ref } from "vue";

// 分页钩子函数
interface Options {
  page?: number;
  size?: number;
  fetchFun: (_arg: any) => Promise<any>;
  params?: Record<any, any>;
  firstLoading?: boolean;
  beforeRequest?(params: Record<any, any>): Record<any, any>;
  afterRequest?(res: Record<any, any>): void;
}

export function usePaging(options: Options) {
  console.log("Options", options);

  const {
    page = 1,
    size = 15,
    fetchFun,
    params = {}, // 包含condition 属性 ,用于判断是否执行
    firstLoading = false,
    beforeRequest,
    afterRequest,
  } = options;
  if (params.condition == undefined) {
    params.condition = true;
  }
  // 记录分页初始参数
  const paramsInit: Record<any, any> = Object.assign({}, toRaw(params));
  // 分页数据
  const pager = reactive({
    page,
    size,
    loading: firstLoading,
    count: 0,
    lists: [] as any[],
    extend: {} as Record<any, any>,
  });
  // 请求分页接口
  const getLists = () => {
    console.log("params", params);

    if (!params.condition) {
      console.log("条件不成立不执行查询");
      return Promise.resolve({
        code: 200,
        data: [],
        message: "操作成功",
        page: 0,
        pageIndex: 1,
        pageSize: 10,
        success: true,
        total: 0,
      });
    }
    pager.loading = true;
    let requestParams = params;
    if (isFunction(beforeRequest)) {
      requestParams = beforeRequest(params);
    }

    return fetchFun({
      pageNum: pager.page,
      pageSize: pager.size,
      ...requestParams,
    })
      .then((res: any) => {
        console.log("usePaging获取数据为,", res);

        pager.count = res?.total;
        pager.lists = res?.data;
        pager.extend = res?.extend;
        if (isFunction(afterRequest)) {
          afterRequest(res);
        }
        return Promise.resolve(res);
      })
      .catch((err: any) => {
        return Promise.reject(err);
      })
      .finally(() => {
        pager.loading = false;
      });
  };
  // 重置为第一页
  const resetPage = () => {
    pager.page = 1;
    getLists();
  };
  // 重置参数
  const resetParams = () => {
    Object.keys(paramsInit).forEach((item) => {
      params[item] = paramsInit[item];
    });
    getLists();
  };
  return {
    pager,
    getLists,
    resetParams,
    resetPage,
  };
}
